import { knowledgeInfoApi } from '#/api';

// 文件的知识片段结构
interface FileSegment {
  id: string;
  name: string;
  description: string;
}

interface FileTag {
  fileId: string;
  fileName: string;
  type: null | string;
  location: null | string[];
  attr: null | string[];
  other: null | string[];
}

/**
 * Converts a list of FileTag objects to a list of FileSegment objects.
 *
 * @param {FileTag[]} fileTagList - The list of FileTag objects to be converted.
 * @return {FileSegment[]} The list of converted FileSegment objects.
 */
export const fileTag2FileSegment = (fileTagList: FileTag[]): FileSegment[] => {
  return fileTagList.map((tag) => {
    // 如果是 null 或 []，使用 '无' 作为默认值
    const { type, location, attr, other } = tag;
    const typeStr = type || '无';
    const locationStr =
      location && location.length > 0 ? location.join('、') : '无';
    const attrStr = attr && attr.length > 0 ? attr.join('、') : '无';
    const otherStr = other && other.length > 0 ? other.join('、') : '无';

    // 拼接 description 字段
    const description = `类型：${typeStr}；地理位置：${locationStr}；应用特性：${attrStr}；其他信息：${otherStr}`;

    // 返回转换后的 FileSegment 对象
    return {
      id: tag.fileId,
      name: tag.fileName,
      description,
    };
  });
};

export const getKnowledgeInfo = async () => {
  try {
    const {
      data: { datasetId, documentId },
    } = (await knowledgeInfoApi.getKnowledgeInfo()) || { data: {} };
    return { datasetId, documentId };
  } catch (error) {
    console.error(error);
    return { datasetId: null, documentId: null };
  }
};
